package com.dys.testdatetime13;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Period;

/**
 * @className: com.dys.testdatetime13.DurationAndPeriod
 * @description: JDK8中提供了两个工具类Duration/Period：计算日期时间差
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/03 下午10:10
 */
public class DurationAndPeriod {
    /**
     * 这是一个main()方法,是应用程序的入口
     *
     * @param args 方法的形参列表,是一个字符串数组,包含了从命令行传递给程序的参数
     */
    public static void main(String[] args) {
        dateDifference();
    }

    /**
     * 计算时间差
     */
    private static void calculationTimeDifference() {
        // 1.获取当前时间
        LocalTime nowTime = LocalTime.now();
        // 2.指定时间格式
        LocalTime time = LocalTime.of(12, 30, 55);
        System.out.println("当前时间：" + nowTime);
        System.out.println("指定时间: " + time);

        // 通过Duration来计算时间差
        Duration between = Duration.between(time, nowTime);
        System.out.println("相差天数：" + between.toDays());
        System.out.println("相差小时数：" + between.toHours());
        System.out.println("相差分钟数：" + between.toMinutes());
        System.out.println("相差秒数：" + between.toSeconds());
    }

    /**
     * 计算日期差
     */
    private static void dateDifference() {
        // 1.获取当前日期
        LocalDate nowDate = LocalDate.now();
        // 2.指定日期
        LocalDate date = LocalDate.of(1998, 11, 26);
        // 计算日期差
        Period between = Period.between(date, nowDate);
        System.out.println("相差年数：" + between.getYears());
        System.out.println("相差月数：" + between.getMonths());
        System.out.println("相差天数：" + between.getDays());
    }
}
